package com.eyas.business.controllor.userinfo;

import com.eyas.business.model.jdbcEntity.table.ChildrenInfo;
import com.eyas.business.model.jdbcEntity.table.UserInfo;
import com.eyas.business.model.pojo.userinfo.QueryPageParmDTO;
import com.eyas.business.model.pojo.userinfo.QueryPageResultDTO;
import com.eyas.business.service.userinfo.UserInfoService;
import com.eyas.core.model.page.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/6 15:30
 * @Description:
 */
@Controller
@RequestMapping("/eyas/userinfo/")
public class UserInfoAction {
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/queryUserInfo")
    @ResponseBody
    public PageResult<QueryPageResultDTO> queryUserInfo(QueryPageParmDTO dto,
                                                            @RequestParam(defaultValue = "1") int page,
                                                            @RequestParam(defaultValue = "10") int limit) {
        PageResult<QueryPageResultDTO> pageResult = userInfoService.queryUserInfo(dto,page-1,limit);
        return pageResult;
    }

    @GetMapping("/queryChildrenInfoByUser")
    @ResponseBody
    public PageResult<ChildrenInfo> queryChildrenInfoByUser(Integer userid,
                                                            @RequestParam(defaultValue = "1") int page,
                                                            @RequestParam(defaultValue = "10") int limit) {
        PageResult<ChildrenInfo> pageResult = userInfoService.queryChildrenInfoByUser(userid,page-1,limit);
        return pageResult;
    }

    @PostMapping("/changeValidflag")
    @ResponseBody
    public String changeValidflag(int userid) {
        userInfoService.changeValidflag(userid);
        return "{\"success\":true}";
    }

    @GetMapping("/edit")
    public String edit(Integer userid, Model model) {
        if(userid!=null&&userid!=0) {
            UserInfo userInfo = this.userInfoService.getUserInfoById(userid);
            QueryPageResultDTO pageResultDTO = new QueryPageResultDTO();
            BeanUtils.copyProperties(userInfo,pageResultDTO);
            model.addAttribute("userInfo",pageResultDTO);
            if(userInfo.getDeadline()!=null)
                model.addAttribute("deadline_str",new SimpleDateFormat("yyyy-MM-dd").format(userInfo.getDeadline()));
        }
        return "/userinfo/edit";
    }

    @GetMapping("/editChildren")
    public String editChildren(Integer cid,Integer userid,Model model) {
        if(cid!=null&&cid!=0) {
            ChildrenInfo childrenInfo = this.userInfoService.getChildInfoByCid(cid);
            model.addAttribute("childrenInfo",childrenInfo);
            if(childrenInfo.getBirthday()!=null)
                model.addAttribute("birthday_str",new SimpleDateFormat("yyyy-MM-dd").format(childrenInfo.getBirthday()));
        }else {
            ChildrenInfo childrenInfo = new ChildrenInfo();
            childrenInfo.setUserid(userid);
            model.addAttribute("childrenInfo",childrenInfo);
        }
        return "/userinfo/editChildren";
    }

    @PostMapping("/saveUserInfo")
    @ResponseBody
    public String saveUserInfo(QueryPageParmDTO dto, HttpSession session) {
        userInfoService.saveUserInfo(dto,session);
        return "{\"success\":true}";
    }

    @PostMapping("/saveChildrenInfo")
    @ResponseBody
    public String saveChildrenInfo(ChildrenInfo childrenInfo) {
        userInfoService.saveChildrenInfo(childrenInfo);
        return "{\"success\":true}";
    }

    @PutMapping("/changeClassFollow")
    @ResponseBody
    public String changeClassFollow(QueryPageParmDTO dto) {
        userInfoService.changeClassFollow(dto);
        return "{\"success\":true}";
    }

}
